//公告切换
var notices = document.querySelectorAll('.informationbar .notice>li');
notices.forEach(function(notice) {
    notice.onmouseenter = function () {
        notices.forEach(function(li) {
            li.classList.remove('active');
        })
        this.classList.add('active');
    }
})


// 图书排行榜切换
var ranks = document.querySelectorAll('.books-ebooks .books-ranking-list>li');
ranks.forEach(function(rank) {
    rank.onmouseenter = function () {
        ranks.forEach(function(li) {
            li.classList.remove('active');
        })
        this.classList.add('active');
    }
})


// 图书畅销排行榜口风琴效果类增删
var titles1 = document.querySelectorAll('#bestsellers-Books .title');
titles1.forEach(function(title){
    title.onmouseenter= function(){
        titles1.forEach(function(t){
            t.classList.remove('active');
        })
        this.classList.add('active');
    }
})
// 图书新书排行榜口风琴效果类增删
var titles2 = document.querySelectorAll('#childrens-book-list .title');
titles2.forEach(function(title){
    title.onmouseenter= function(){
        titles2.forEach(function(t){
            t.classList.remove('active');
        })
        this.classList.add('active');
    }
})


// 模式一标题之图书切换
var listitems1 = document.querySelectorAll('.module-title1>.books-title-listitem');
listitems1.forEach(function(listitem) {
    listitem.onmouseenter = function () {
        listitems1.forEach(function(li) {
            li.classList.remove('active');
        })
        this.classList.add('active');
    }
})
// 模式一标题之服装切换
var listitems2 = document.querySelectorAll('.module-title1>.clothing-title-listitem');
listitems2.forEach(function(listitem) {
    listitem.onmouseenter = function () {
        listitems2.forEach(function(li) {
            li.classList.remove('active');
        })
        this.classList.add('active');
    }
})


// 关闭按钮
var btn_close = document.getElementById('btn-close');
btn_close.onclick = function () {
    this.parentElement.parentElement.style.display = 'none';
}


// 页面滚动出去后显示锚点链接,显示吸顶搜索
var e1 = document.documentElement || document.body;
var miaosha_top = document.querySelector('.main .miaosha').offsetTop;
window.onscroll = function() {
    var scroll_top = e1.scrollTop;
    if (scroll_top > 0) {
        document.querySelector('.fixed-content .anchor-link-box').style.display = 'block';
        if(scroll_top >= miaosha_top) {
            document.querySelector('.fixed-content .ceiling-search').style.display = 'block';
        }
        else{
            document.querySelector('.fixed-content .ceiling-search').style.display = 'none';
        }
    } 
    else {
        document.querySelector('.fixed-content .anchor-link-box').style.display = 'none';
        document.querySelector('.fixed-content .ceiling-search').style.display = 'none';
    }
}


//厂商周切换
var miaosha_right_lists = document.querySelectorAll('.miaosha-right .miaosha-right-top ul>li');
var miaosha_right_ul = document.querySelectorAll('.miaosha-right .miaosha-right-bottom>ul');
for(var i = 0; i < miaosha_right_lists.length; i++) {
    miaosha_right_lists[i].index = i;
    miaosha_right_lists[i].addEventListener('mouseenter', function() {
        clearInterval(timerId1);
        for(var i = 0; i < miaosha_right_lists.length; i++) {
            miaosha_right_lists[i].classList.remove('active');
            miaosha_right_ul[i].classList.remove('show');
        }
        this.classList.add('active');
        miaosha_right_ul[this.index].classList.add('show');
        timerId1 = setInterval(automatic_switching, 5000);
    })
}
// 自动切换效果
var timerId1 = setInterval(automatic_switching, 5000);
function automatic_switching() {
    var index;
    // 判断是否包含active类
    for(var i = 0, len=miaosha_right_lists.length; i<len; i++) {
        var a = miaosha_right_lists[i].className.split(' ').indexOf('active');
        if (a !== -1) {
            index = i;
            break;
        }
    }
    miaosha_right_lists[index].classList.remove('active');
    miaosha_right_ul[index].classList.remove('show');
    if(index === miaosha_right_lists.length-1) {
        miaosha_right_lists[0].classList.add('active');
        miaosha_right_ul[0].classList.add('show');
    }
    else {
        miaosha_right_lists[index + 1].classList.add('active');
        miaosha_right_ul[index + 1].classList.add('show');
    }
}


// ---------------------轮播效果1-------------------------
// 序号划过切换图片
var carousel_box = document.getElementById('carousel-box');
var carousel_map = document.querySelector('#carousel-box .carousel-map');
var lists = carousel_map.children;
var carousel_index = document.querySelector('#carousel-box .carousel-index');
var indexs = carousel_index.children;
for(var i = 0, len=indexs.length; i<len; i++) {
    indexs[i].index = i;
    indexs[i].addEventListener('mouseenter', fn1); 
}
function fn1() { 
    for(var i = 0, len=indexs.length; i<len; i++) {
        indexs[i].classList.remove('active');
        lists[i].classList.remove('show');
    }
    this.classList.add('active');
    var index = parseInt(this.index);
    lists[index].classList.add('show');
}

function next() {
    var index;
    // 判断是否包含active类
    for(var i = 0, len=indexs.length; i<len; i++) {
        var a = indexs[i].className.split(' ').indexOf('active');
        if (a !== -1) {
            index = i;
            break;
        }
    }
    indexs[index].classList.remove('active');
    lists[index].classList.remove('show');
    if(index === indexs.length-1) {
        indexs[0].classList.add('active');
        lists[0].classList.add('show');
    }
    else {
        indexs[index + 1].classList.add('active');
        lists[index + 1].classList.add('show');
    } 
}
// 自动更换图片
var timerId = setInterval(next,5000);

// 获取左右箭头
var carouselbtn_left = document.querySelector('#carousel-box .carouselbtn-left');
var carouselbtn_right = document.querySelector('#carousel-box .carouselbtn-right');
// 上一个/左边箭头点击事件
carouselbtn_left.onclick = function () {
    var index;
    // 判断是否包含active类
    for(var i = 0, len=indexs.length; i<len; i++) {
        var a = indexs[i].className.split(' ').indexOf('active');
        if (a !== -1) {
            index = i;
            break;
        }
    }
    indexs[index].classList.remove('active');
    lists[index].classList.remove('show');
    if(index === 0) {
        indexs[indexs.length-1].classList.add('active');
        lists[indexs.length-1].classList.add('show');
    }
    else {
        indexs[index - 1].classList.add('active');
        lists[index - 1].classList.add('show');
    }
}
// 下一个/右边箭头点击事件
carouselbtn_right.onclick = function () {
    next();
}

//划过图片，暂停自动播放，划出又自动播放
carousel_box.onmouseenter = function() {
    clearInterval(timerId);
}
carousel_box.onmouseleave = function() {
    timerId = setInterval(next,5000);
}


// ---------------------轮播效果2-------------------------
// 序号点击切换图片
var map = document.querySelector('#informationbar-bottom .map');
var lists1 = document.querySelector('#informationbar-bottom ul').children;
var xuhaos = document.querySelector('#informationbar-bottom .xuhao').children;
for(var i = 0; i< xuhaos.length; i++) {
    xuhaos[i].index = i;
    xuhaos[i].addEventListener('click', fn2); 
}
function fn2() {  
    for(i = 0, len=xuhaos.length; i<len; i++) {
        // 清除所有点红色背景
        xuhaos[i].classList.remove('active');         
    }
    this.classList.add('active');
}
